package Nets

import (
	"github.com/gin-gonic/gin"
	"chaoyer/ghost_assist/base/code"
	"database/sql"
)

type Result struct {
	Code    int         `json:"code"`
	Data    interface{} `json:"data,omitempty"`
	Success bool        `json:"success"`
}

func SendSucess(c *gin.Context) {
	result := &Result{1, nil, true}
	callback(c, result)
}

func SendData(c *gin.Context, data interface{}) {
	result := &Result{1, data, true}
	callback(c, result)
}

func SendMessage(c *gin.Context, code int, data interface{}) {
	result := &Result{code, data, false}
	callback(c, result)
}

func SendError(c *gin.Context, code int) {
	result := &Result{code, nil, false}
	callback(c, result)
}

func CheckError(c *gin.Context, err error, data string) bool {
	if err == nil {
		return false
	}
	result := &Result{code.SEVER_ERROR, nil, false}
	if err == sql.ErrNoRows {
		result.Code = code.RESOURCE_NOT_FOUND
		result.Data = data
	}
	callback(c, result)
	return true
}

func callback(c *gin.Context, result *Result) {
	c.JSON(200, result)
}
